package com.dalongxin.work.page.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.dalongxin.work.BaseApplication;
import com.dalongxin.work.R;

import org.xutils.x;

import java.util.List;

public class PictureAdapter extends BaseAdapter {

    private Context mContext;
    public static final int MAX_NUM = 10;
    private List<String> mImgPaths;
    private Activity mActivity;
    private Fragment mFragment;
    private LayoutInflater inflater;
    private OnOperationListener mListener;

    private boolean isEdit = true;

    public PictureAdapter(Activity activity, List<String> imgPaths){
        this.mActivity=activity;
        this.mImgPaths=imgPaths;
        this.inflater=LayoutInflater.from(activity);
    }

    public PictureAdapter(Fragment fragment, List<String> imgPaths){
        this.mFragment=fragment;
        this.mImgPaths=imgPaths;
        this.inflater=LayoutInflater.from(fragment.getContext());
    }

    public PictureAdapter(Context context, List<String> imgPaths){
        this.mContext=context;
        this.mImgPaths=imgPaths;
        this.inflater=LayoutInflater.from(context);
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    @Override
    public int getCount() {
        if(mImgPaths == null || mImgPaths.size() == 0) {
            return 0;
        }else if(mImgPaths.size() < MAX_NUM) {
            return mImgPaths.size() + 1;//1-8张的加个加号
        }else {
            return MAX_NUM;
        }
    }

    @Override
    public String getItem(int i) {
        return mImgPaths.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            view=inflater.inflate(R.layout.item_show_pics,null);
            holder=new ViewHolder();
            holder.ivPicture= (ImageView) view.findViewById(R.id.show_selected_pic_item_siv_picture);
            holder.ivDelete= (ImageView) view.findViewById(R.id.show_selected_pic_item_iv_delete);
            holder.ivAdd= (ImageView) view.findViewById(R.id.show_selected_pic_item_iv_add);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }
        if(i!=mImgPaths.size()){
            if(mActivity!=null){
                x.image().bind(holder.ivPicture, getItem(i),
                        BaseApplication.getInstance().getDefaultImageOption()
                                .setFailureDrawableId(R.mipmap.cloud_disk_pic_new).build());
            }else if(mFragment!=null){
                x.image().bind(holder.ivPicture, getItem(i),
                        BaseApplication.getInstance().getDefaultImageOption()
                                .setFailureDrawableId(R.mipmap.cloud_disk_pic_new).build());
            }else {
                x.image().bind(holder.ivPicture, getItem(i),
                        BaseApplication.getInstance().getDefaultImageOption()
                                .setFailureDrawableId(R.mipmap.cloud_disk_pic_new).build());
            }
            holder.ivAdd.setVisibility(View.GONE);
            if(isEdit) {
                holder.ivDelete.setVisibility(View.VISIBLE);
            }else {
                holder.ivDelete.setVisibility(View.GONE);
            }
        }else {
            if(isEdit) {
                holder.ivAdd.setVisibility(View.VISIBLE);
            }else {
                holder.ivAdd.setVisibility(View.GONE);
            }
//            holder.ivPicture.setImageResource(0);
            if(mActivity!=null){
                holder.ivPicture.setBackgroundResource(R.color.transparent);
                //Glide.with(mActivity).load(R.color.transparent).centerCrop().into(holder.ivPicture);
            }else if(mFragment!=null){
                holder.ivPicture.setBackgroundResource(R.color.transparent);
                //Glide.with(mFragment).load(R.color.transparent).centerCrop().into(holder.ivPicture);
            }else {
                holder.ivPicture.setBackgroundResource(R.color.transparent);
                //Glide.with(mContext).load(R.color.transparent).centerCrop().into(holder.ivPicture);
            }
            holder.ivDelete.setVisibility(View.GONE);
        }

        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mImgPaths.remove(i);
                notifyDataSetChanged();
                if(mListener!=null){
                    mListener.onPicDelete(i);
                }
            }
        });
        return view;
    }

    private class ViewHolder{
        ImageView ivPicture;
        ImageView ivDelete;
        ImageView ivAdd;
    }

    public interface OnOperationListener{
        void onPicDelete(int position);
        void onAddClicked();
    }

    public void setOnOperationListener(OnOperationListener listener){
        this.mListener=listener;
    }
}
